<?php
class IndexAction extends BaseAction{
    
	function _initialize(){
    	parent::_initialize();
    }
	
    //用户个人首页
	//需要做的处理有：	1.当前订单状态。
	//					2.个人信息是否完善
	//					3.待定....
	public function index(){
		
		//现金余额
		$uaccount = service('UserAccount')->get4User();
    	$this->assign('uaccount', $uaccount);
    	
    	//未使用的礼券
    	if(!empty($this->user['mobile']))$notUsedCouponNo = service('Coupon')->countByMobile($this->user['mobile'],'unused');
		$this->assign('notUsedCouponNo', $notUsedCouponNo);
		
		//最近的5个礼物
		$newCoupons = service('Coupon')->searchByMobile($this->user['mobile'],'all',true,7);
		$this->assign('newCoupons', $newCoupons);
		
		//未付款的订单数
		$upayOrderNo = service('Order')->count4User('unpay');
		$this->assign('upayOrderNo', $upayOrderNo);
		
		//最近送礼人
		$senderIds = service('Coupon')->select4UserSenderByMobile($this->user['mobile'],10);
		$this->assign('senderIds', $senderIds);
		
    	$this->display();
	}
	
	public function sendList(){
		$this->display();
	}
	
	public function ajxSendList(){
		$this->display('send_list_box');
	}
	
	public function ajxDeleteOrder(){
		$order_id = t($_REQUEST['order_id']);
		$result = service('Order')->delete4User($order_id);	
		exit(json_encode($result));
	}
	
	public function receiveList(){
		$this->display();
	}
	
	public function ajxReceiveList(){
		$this->display('receive_list_box');
	}
	
	public function ajxGiftDetail(){
		$coupon_id = $_REQUEST['coupon_id'];
		$this->assign('coupon_id', $coupon_id);
		$this->display('gift_detail_box');
	}
	
	public function tobesendList(){
		$amount = service('Collect')->count4User(Dict::TOBESEND);
		$this->assign('amount', $amount);
		$this->display();
	}
	
	public function desirousList(){
		$amount = service('Collect')->count4User(Dict::DESIROUS);
		$this->assign('amount', $amount);
		$this->display();
	}
	
	public function ajxCollectList(){
		$this->display('collect_list_box');
	}
	
	public function ajxdoCollection(){
		Log::record('ajxdoCollection()请求参数='.parseAry2Str($_REQUEST),Log::DEBUG,Log::FILE);
		Log::save();
		$shelf_id 	= $_REQUEST['shelf_id'];
		$stype 		= $_REQUEST['stype'];
		$result = service('Collect')->add4User($stype,$shelf_id);
		//if($stype==Dict::TOBESEND)$result;
		
		if( isValidEmail($_REQUEST['email']) ){
			global $ts;
			$product = service('Product')->getByShelfId($shelf_id);
			$sendname = empty($ts['user']['realname'])?$ts['user']['uname']:$ts['user']['realname'];
			$sendname = empty($_REQUEST['sendname'])?$sendname:$_REQUEST['sendname'];
			$body = $this->_sendTAEmailBody($product,$sendname);
			if (service('Mail')->send_email($_REQUEST['email'], "{$ts['site']['site_name']}：您的好友 {$sendname} 发给您的！", $body)) {
				$result['status']=1;
				$result['msg']=$result['msg']." 邮件发送成功！";
			}else {
				$result['msg']=$result['msg']." 邮件发送失败！";
			}	
		}
		
		exit(json_encode($result));
	}
	
	public function ajxgetCollections(){
		$shelf_ids 	= $_REQUEST['shelf_ids'];
		$stype 		= $_REQUEST['stype'];
		$shelf_ids = explode(',',$shelf_ids);
		//dump($shelf_ids);
		if(!empty($shelf_ids)){
			$collections = service('Collect')->select4UserIfCollect($shelf_ids,$stype);
		}
		//dump($collections);
		exit(json_encode(array('status'=>1,'data'=>$collections)));
	}
	
	public function ajxcancelCollection(){
		$shelf_id = $_REQUEST['shelf_id'];
		$stype 		= $_REQUEST['stype'];
		$result = service('Collect')->delete4User($stype,$shelf_id);
		exit(json_encode($result));
	}
	
	public function ajxSendWB(){
		$type = h($_REQUEST['type']);
		$text = h($_REQUEST['text']);
		$this->_doSendWeibo($_SESSION['mid'],array('text'=>$text,'pic'=>null),$type);
		
	}
	
	
	/**
	 * 
	 * @param unknown_type $uid				用户ID
	 * @param unknown_type $data			发送内容$data['text'] $data['pic']
	 * @param unknown_type $from
	 * @param unknown_type $type_data
	 * @param unknown_type $sync
	 * @param unknown_type $from_data
	 */
    private function _doSendWeibo($uid,$data,$platform_type){
        if(!$data['text']){
        	return false;
        }
     	
        if(mb_strlen($data['text'],'UTF8')>140){
        	return false;
        }
		
	    $opt = M('login')->where("uid=".$uid." AND type='".$platform_type."'")->field('oauth_token,oauth_token_secret,is_sync')->find();
		dump($opt);
		include_once( QUICK_ADDONE_PATH."/plugins/login/{$platform_type}.class.php" );
		
		$platform = new $platform_type();
		if(!empty($data['pic'])){
			$result = $platform->upload($data['text'],SITE_URL.'/data/uploads/'.$data['pic'],$opt);
		}else{
			$result = $platform->update($data['text'],$opt);
		}
		dump($result);
    }
    
    /**
     * 重新发送礼券
     */
    public function resendCoupon(){
    	$sendRole=$_REQUEST['sendRole'];
    	$id=$_REQUEST['id'];
    	$oid=$_REQUEST['oid'];
    	$result = service('Coupon')->selectCouponReSend4User($id,$oid,$sendRole,$this->user['mobile']);
    	if($result['status']==1){
			//发送可以发送的商品券
			$sendcenter = service('SendCenter');
			$senData = $sendcenter->parseCouponSendData($result['data']);
			$sendcenter->send($senData);
			$result=array('status'=>1,'msg'=>'发送成功！');
		}
    	exit(json_encode($result));
    }
    
    

 private function _sendTAEmailBody($productdata,$sendname){
    	$basePublicPath = SITE_URL.'/Public';
    	$basesiteurl = SITE_URL;
		$goodsurl = SITE_URL.U('Product://Index/detailp',array('shelfid'=>$productdata['shelf_id']));
		$img = SITE_URL.getPhotoFromThumb($productdata['goods_icon'],250,250);
		$shelf_market_price = formatMoney($productdata['shelf_market_price']);
		$shelf_out_price 	= formatMoney($productdata['shelf_out_price']);
		$bsub_bsubpage 	= service('Brand')->search4UserBSub($productdata['partner_id'],$bsub_area_id,$bsub_city_id,null,20);
		$body = <<<EOD
<body>
<div class="blank10"></div>
<div style="width:100%; text-align:center; margin:0 auto; ">
	<div class="" style="font-size: 14px; line-height: 170%;">
		<div style="font-size: 14px; padding: 0pt; height: auto; font-family: 'lucida Grande', Verdana; margin-right: 0px;" >
			<div style="width: 524px; margin: 0 auto; text-align: center;">
				<div style="text-align: left; font-weight: bold;"><b class="green"></b>您的好友{$sendname}发送给您的：</div>
				<div style="text-align: left; font-size: 12px; margin: 4px 0 18px; line-height: 18px;">
					{$sendname}在小心意网站(<a href="{$basesiteurl}" target="_blank" >www.xiaoxinyi.com</a>)
					上看到心仪的商品 【{$productdata['goods_title']}】，TA想到了你，希望你能够实现TA的愿望。
				</div>
			<div>
			<div style="border: 1px solid #488825; -moz-border-radius: 3px; -khtml-border-radius: 3px; -webkit-border-radius: 3px; border-radius: 3px; float: left;">
				<a style="font-size: 14px; width: 150px; border: 2px solid #488825; -moz-border-radius: 3px; -khtml-border-radius: 3px; -webkit-border-radius: 3px; border-radius: 1px; height: 24px; line-height: 24px; cursor: pointer; background: #62A026; color: #FFF; font-weight: bold; border: 1px solid #7CB04A; padding: 0 0 0px; display: block; text-decoration: none; text-align: center;"
					href="{$goodsurl}"	target="_blank">点击送给TA</a>
			</div>
			<div style="float: left; margin: 3px 0 0 15px;">（此邮件为系统发送，无需回复！）</div>
		</div>
		<div class="blank10"></div>
	</div>
	
	<div style="text-align: center;width: 100%">
		<div style="width: 540px;height:600px;margin:0px auto;background: url('{$basePublicPath}/Images/global/bg_sendTaEmail.jpg') no-repeat transparent;">
			<div class="primary_box">
				<div class="blank10"></div>	
				<!--商品概要信息展示，最右上侧-->
				<ul class="info" style="text-align:left;">
					<!--1. 商品名称-->
					<li class="name"><strong>{$productdata['goods_title']}</strong></li>
					<!--2. 商品品牌以及编号，可以定位到品牌页面，这里的商品品牌是不是使用图标比较好？-->
					<li>商品品牌：{$productdata['partner_title']}</li> 
					<!-- 
					<li>商品编号：<b><span id="goods_id">{$productdata['goods_id']}</span></b></li>
					 -->
					<!--3. 市场价，加删除线-->
					<li>市 场 价： <b class="price">{$shelf_market_price}</b></li>
					<!--4. 小心意价格-->
					<li>心 意 价： <span class="sell">{$shelf_out_price}</span></li>
					<li>兑换地点：
						<a class="info_a" href="{$goodsurl}" target="_blank" title="查看心意券能够兑换的所有店地址">点击查看对换地点</a></li>
					
					<li >商品构成：{$productdata['goods_summary']}</li>
					<li class="br_line clear"></li>
				</ul>
		
		
				<!--商品图片展示，最左上侧-->
				<div class="pic">
					<!--1. 图片盒，目前共四张，用js来控制显示与否-->
					<div id="pic_box">
						<div style="float: left;text-align: center;overflow: hidden;border:0px solid #EBEBEB;">
							<a 	href="{$goodsurl}"  class="goods_b" target="_blank" title="{$productdata['goods_title']}" >
								<img src="{$img}" alt="{$productdata['goods_title']}">	
							</a>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
<style>
HTML { }
BODY { margin: 0; color: #494949;font-size:12px; font-family:"微软雅黑"; /**font:9pt Tahoma,"微软雅黑", Arial,Helvetica,Sans-Serif;**/ line-height:14px; letter-spacing:0;}
body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form,fieldset,legend,input,button,textarea,p,blockquote,th,td,select{ padding: 0; margin: 0; }
UL,LI { padding:0; margin:0; list-style:none;}
IMG { border: none; padding: 0; margin: 0;}
input,img,select {vertical-align:middle;}

/* 公共样式表 */
* {margin:0;padding:0;}
input, select, option, textarea{margin:0;padding:0; font-size:12px;}
h1, h2, h3, h4, h5, h6 {font-size:14px;}
input{ padding:0px; margin:0px; font-size:12px;}

A {color: #1F71CB;font-size:12px;text-decoration:none}
A:link { color:#1F71CB; text-decoration: none;}
A:visited { color:#1F71CB; text-decoration: none;}
A:hover { color:#FF9900;}
.primary_box {
    float: left;
    margin: 10px 10px 10px 17px;
    width: 510px;
}
.pic {
    float: left;
    width: 250px;
}
#pic_box {
    height: 250px;
    overflow: hidden;
}
#pic_box div {
    display: block;
    height: 250px;
    left: 0;
    overflow: hidden;
    padding: 0;
    top: 0;
    width: 250px;
}
.info {
    color: #999999;
    display: block;
    float: right;
    font-family: Tahoma,Arial,Helvetica,sans-serif,SimSun;
    line-height: 28px;
    padding-right: 5px;
    width: 240px;
}
.info b {
    color: #999999;
    font-weight: normal;
}
.info strong {
    color: #666666;
}
.brand {
    color: #999999;
    font-size: 14px;
}
.name {
    border-bottom: 1px dashed #DEDEDE;
    color: #000000;
    font-size: 16px;
    margin-bottom: 5px;
    padding-bottom: 3px;
    padding-right: 5px;
}
.price {
    font-family: Arial,Helvetica,sans-serif;
    font-size: 12px;
    font-style: italic;
    text-decoration: line-through;
}
.sell {
    color: #D30200;
    font-family: Arial,Helvetica,sans-serif;
    font-size: 26px;
    font-weight: bold;
    line-height: 30px;
    padding-right: 5px;
}

.blank10 {
    clear: both;
    height: 10px;
    list-style: none outside none;
    margin: 0;
    overflow: hidden;
    padding: 0;
}
</style>
</body>
EOD;

		return $body;
    }

}
?>